| Conditions | 18 |
| Paths | 3960 |
| Total Lines | 158 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ComposeOpenPgp.js ➔ ... ➔ Utils.createCommand-1 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | |||
| 85 | }); |
||
| 86 | return _.compact(_.flatten(opts, true)); |
||
| 87 | }); |
||
| 88 | |||
| 89 | this.submitRequest = ko.observable(false); |
||
| 90 | |||
| 91 | this.resultCallback = null; |
||
| 92 | |||
| 93 | // commands |
||
| 94 | this.doCommand = createCommand(() => { |
||
| 95 | |||
| 96 | let |
||
| 97 | result = true, |
||
| 98 | privateKey = null, |
||
| 99 | aPublicKeys = []; |
||
| 100 | |||
| 101 | this.submitRequest(true); |
||
| 102 | |||
| 103 | if (result && this.sign()) |
||
| 104 | { |
||
| 105 | if (!this.signKey()) |
||
| 106 | { |
||
| 107 | this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND')); |
||
| 108 | result = false; |
||
| 109 | } |
||
| 110 | else if (!this.signKey().key) |
||
| 111 | { |
||
| 112 | this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', { |
||
| 113 | 'EMAIL': this.signKey().email |
||
| 114 | })); |
||
| 115 | |||
| 116 | result = false; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (result) |
||
| 120 | { |
||
| 121 | const privateKeys = this.signKey().key.getNativeKeys(); |
||
| 122 | privateKey = privateKeys[0] || null; |
||
| 123 | |||
| 124 | try |
||
| 125 | { |
||
| 126 | if (privateKey) |
||
| 127 | { |
||
| 128 | privateKey.decrypt(pString(this.password())); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | catch (e) |
||
| 132 | { |
||
| 133 | privateKey = null; |
||
| 134 | } |
||
| 135 | |||
| 136 | if (!privateKey) |
||
| 137 | { |
||
| 138 | this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND')); |
||
| 139 | result = false; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if (result && this.encrypt()) |
||
| 145 | { |
||
| 146 | if (0 === this.encryptKeys().length) |
||
| 147 | { |
||
| 148 | this.notification(i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND')); |
||
| 149 | result = false; |
||
| 150 | } |
||
| 151 | else if (this.encryptKeys()) |
||
| 152 | { |
||
| 153 | aPublicKeys = []; |
||
| 154 | |||
| 155 | _.each(this.encryptKeys(), (oKey) => { |
||
| 156 | if (oKey && oKey.key) |
||
| 157 | { |
||
| 158 | aPublicKeys = aPublicKeys.concat(_.compact(_.flatten(oKey.key.getNativeKeys()))); |
||
| 159 | } |
||
| 160 | else if (oKey && oKey.email) |
||
| 161 | { |
||
| 162 | this.notification(i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND_FOR', { |
||
| 163 | 'EMAIL': oKey.email |
||
| 164 | })); |
||
| 165 | |||
| 166 | result = false; |
||
| 167 | } |
||
| 168 | }); |
||
| 169 | |||
| 170 | if (result && (0 === aPublicKeys.length || this.encryptKeys().length !== aPublicKeys.length)) |
||
| 171 | { |
||
| 172 | result = false; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if (result && this.resultCallback) |
||
| 178 | { |
||
| 179 | _.delay(() => { |
||
| 180 | |||
| 181 | let pgpPromise = null; |
||
| 182 | |||
| 183 | try |
||
| 184 | { |
||
| 185 | if (privateKey && 0 === aPublicKeys.length) |
||
| 186 | { |
||
| 187 | pgpPromise = PgpStore.openpgp.sign({ |
||
| 188 | data: this.text(), |
||
| 189 | privateKeys: [privateKey] |
||
| 190 | }); |
||
| 191 | } |
||
| 192 | else if (privateKey && 0 < aPublicKeys.length) |
||
| 193 | { |
||
| 194 | pgpPromise = PgpStore.openpgp.encrypt({ |
||
| 195 | data: this.text(), |
||
| 196 | publicKeys: aPublicKeys, |
||
| 197 | privateKeys: [privateKey] |
||
| 198 | }); |
||
| 199 | } |
||
| 200 | else if (!privateKey && 0 < aPublicKeys.length) |
||
| 201 | { |
||
| 202 | pgpPromise = PgpStore.openpgp.encrypt({ |
||
| 203 | data: this.text(), |
||
| 204 | publicKeys: aPublicKeys |
||
| 205 | }); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | catch (e) |
||
| 209 | { |
||
| 210 | log(e); |
||
| 211 | |||
| 212 | this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { |
||
| 213 | 'ERROR': '' + e |
||
| 214 | })); |
||
| 215 | } |
||
| 216 | |||
| 217 | if (pgpPromise) |
||
| 218 | { |
||
| 219 | try |
||
| 220 | { |
||
| 221 | pgpPromise.then((mData) => { |
||
| 222 | this.resultCallback(mData.data); |
||
| 223 | this.cancelCommand(); |
||
| 224 | }).catch((e) => { |
||
| 225 | this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { |
||
| 226 | 'ERROR': '' + e |
||
| 227 | })); |
||
| 228 | }); |
||
| 229 | } |
||
| 230 | catch (e) |
||
| 231 | { |
||
| 232 | this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { |
||
| 233 | 'ERROR': '' + e |
||
| 234 | })); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | this.submitRequest(false); |
||
| 239 | |||
| 240 | }, Magics.Time20ms); |
||
| 241 | } |
||
| 242 | else |
||
| 243 | { |
||
| 461 |